package com.zlf.sb.demo.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zlf.sb.demo.config.EventListenerExecutor;
import com.zlf.sb.demo.entity.CreditRecord;
import com.zlf.sb.demo.mapper.CreditRecordMapper;
import com.zlf.sb.demo.service.ICreditRecordService;
import com.zlf.sb.demo.vo.CreditsRecordVO;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

@Service
public class CreditRecordServiceImpl extends ServiceImpl<CreditRecordMapper, CreditRecord> implements ICreditRecordService {

    /*private final TransactionDefinition transactionDefinition;

    private final DataSourceTransactionManager transactionManager;

    private final CreditRecordMapper creditRecordMapper;

    private final MemberMapper memberMapper;*/

    public CreditRecordServiceImpl() {
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void dealData(CopyOnWriteArrayList<CreditsRecordVO> creditsRecordVOS, CountDownLatch countDownLatch) {
        if (CollectionUtil.isNotEmpty(creditsRecordVOS)) {
            EventListenerExecutor.execute(() -> {
                doWork(creditsRecordVOS);
                countDownLatch.countDown();
            });
        }
    }

    private void doWork(CopyOnWriteArrayList<CreditsRecordVO> creditLogVOS) {
        /*try {
            List<CreditRecord> result = new ArrayList<>();
            for (CreditsRecordVO creditLogVO : creditLogVOS) {
                CreditRecord creditRecord = new CreditRecord();
                creditRecord.setId(Long.valueOf(creditLogVO.getId()));
                creditRecord.setContent(creditLogVO.getRemark() != null ? creditLogVO.getRemark() : "");
                if (Objects.nonNull(creditLogVO.getNum())) {
                    creditRecord.setNum(BigDecimal.valueOf(creditLogVO.getNum()));
                    if (creditLogVO.getNum() > 0) {
                        creditRecord.setFlowType(1);
                    } else if (creditLogVO.getNum() < 0) {
                        creditRecord.setFlowType(1);
                    }
                } else {
                    continue;
                }
                creditRecord.setCreditType(creditLogVO.getCredittype());
                creditRecord.setStatus(1);
                if (Objects.nonNull(creditLogVO.getUid())) {
                    creditRecord.setMemberId(Long.valueOf(creditLogVO.getUid()));
                } else {
                    continue;
                }
                Integer createtime = creditLogVO.getCreatetime();
                if (Objects.nonNull(createtime)) {
                    LocalDateTime localDateTime = LocalDateTime.ofInstant(
                            Instant.ofEpochSecond(createtime), ZoneOffset.ofHours(8)
                    );
                    creditRecord.setCreateTime(localDateTime);
                }
                creditRecord.setBalanceSnapshot(BigDecimal.valueOf(-1));
                creditRecord.setDeleted(0);
                creditRecord.setIsExp(0);
                creditRecord.setFromId(0L);
                creditRecord.setOpType(0);
                creditRecord.setOrderNo("");
                creditRecord.setUpdateTime(LocalDateTime.now());
                result.add(creditRecord);
            }
            log.info("插入数据条数:{}", result.size());
            Integer cr = creditRecordMapper.insertBatchSomeColumn(result);
            if (cr > 0) {
                log.info("插入积分记录ok");
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("插入积分记录异常:{}", ExceptionUtils.getMessage(e));
        }*/
    }
}
